package com.wkw.jkyf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wkw.jkyf.mapper.DruginfoMapper;
import com.wkw.jkyf.mapper.ReturnSupplierMapper;

import com.wkw.jkyf.pojo.Billinfo;
import com.wkw.jkyf.pojo.Druginfo;
import com.wkw.jkyf.pojo.Problem;
import com.wkw.jkyf.pojo.Returnsupplier;
import com.wkw.jkyf.service.IReturnsupplierService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

//退货给供应商
@Service
public class ReturnsupplierServiceImpl  extends ServiceImpl<ReturnSupplierMapper, Returnsupplier> implements IReturnsupplierService {

    @Resource
    private ReturnSupplierMapper returnsupplierMapper;
    @Resource
    private DruginfoMapper druginfoMapper;


    @Override
    public IPage<Returnsupplier> selectReturnsupplierPage(int pageNum, int pageSize, String param) {
        QueryWrapper<Returnsupplier> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(param)){
            queryWrapper.like("dname",param);
        }
        queryWrapper.orderByDesc("id");
        Page<Returnsupplier> page = new Page<>(pageNum,pageSize);
        return returnsupplierMapper.selectPage(page,queryWrapper);
    }

    @Override
    @Transactional
    public int addReturnsupplier(Returnsupplier returnsupplier) {
        int operateFlag = 0;
        QueryWrapper<Druginfo> drugWrapper = new QueryWrapper<>();
        drugWrapper
                .eq("name",returnsupplier.getDname())
                .eq("number",returnsupplier.getDnumber());
        Druginfo druginfo = druginfoMapper.selectOne(drugWrapper);
        //药品库存数量
        int stock = druginfo.getStock();
        //退回药品数量
        int returnCount = returnsupplier.getDcount();
        if(returnCount>stock){
            return -1;
        }
        druginfo.setStock(stock-returnCount);
        operateFlag += druginfoMapper.updateById(druginfo);
        //得到当前用户对象
        Subject subject = SecurityUtils.getSubject();
        String userName = (String)subject.getSession().getAttribute("userName");
        returnsupplier.setOperator(userName);
        returnsupplier.setCreatetime(new Date());
        returnsupplierMapper.insert(returnsupplier);
        return operateFlag;
    }

    @Override
    @Transactional
    public int editReturnsupplier(Returnsupplier returnsupplier) {
        QueryWrapper<Druginfo> drugWrapper = new QueryWrapper<>();
        drugWrapper
                .eq("name",returnsupplier.getDname())
                .eq("number",returnsupplier.getDnumber());
        Druginfo druginfo = druginfoMapper.selectOne(drugWrapper);

        QueryWrapper<Returnsupplier> returnSupplierWrapper = new QueryWrapper<>();
        returnSupplierWrapper
                .eq("id",returnsupplier.getId());
        //当前数据库的账单信息
        Returnsupplier oldReturnSupplier = returnsupplierMapper.selectOne(returnSupplierWrapper);
        if(!oldReturnSupplier.getDnumber().equals(returnsupplier.getDnumber())||!oldReturnSupplier.getSname().equals(returnsupplier.getSname())){
            return -2;
        }
        //当前数据库的供应商药品数量
        int oldCount = oldReturnSupplier.getDcount();
        //新的供应商药品数量
        int newCount = returnsupplier.getDcount();
        if(newCount<oldCount){
            druginfo.setStock(druginfo.getStock()+(oldCount-newCount));
            druginfoMapper.updateById(druginfo);
        }else if(newCount>oldCount){
            int newSaleCount = newCount - oldCount;
            if(newSaleCount>druginfo.getStock()){
                return -1;
            }
            druginfo.setStock(druginfo.getStock()-newSaleCount);
            druginfoMapper.updateById(druginfo);
        }

        return returnsupplierMapper.updateById(returnsupplier);
    }

    @Override
    public Returnsupplier queryReturnsupplierById(Integer id) {
        return returnsupplierMapper.selectById(id);
    }

    @Override
    @Transactional
    //删除退货给供应商
    public int deleteReturnsupplierById(Integer id) {
        Returnsupplier returnSupplier = returnsupplierMapper.selectById(id);
        //查询出来的数量
        Integer count = returnSupplier.getDcount();
        QueryWrapper<Druginfo> drugWrapper = new QueryWrapper<>();
        drugWrapper
                .eq("name",returnSupplier.getDname())
                .eq("number",returnSupplier.getDnumber());
        Druginfo druginfo = druginfoMapper.selectOne(drugWrapper);
        druginfo.setStock(druginfo.getStock()+count);
        druginfoMapper.updateById(druginfo);
        return returnsupplierMapper.deleteById(id);
    }
}
